
declare let window: any;
const storeName = 'log'
export const OPEN_DB_AND_INIT = (name): Promise<any> => {
    return new Promise((reslove, reject) => {
        let request = window.indexedDB.open(name, 10);

        request.onsuccess = (e) => {
            reslove(e)
        };

        request.onerror = (error) => {
            reject(error)
        };

        request.onupgradeneeded = function (event) {
            let db = event.target.result;
            if (!db.objectStoreNames.contains(storeName)) {
                // 创建仓库对象（创建表格）
                // 这里我将主键设置为id
                let objectStore = db.createObjectStore(storeName, {
                    keyPath: 'id',
                });
            }
        };
    })

}

// 初始化任务列表表
// 主键id            id                 varchar(32)
// 当前url           url                varchar(256)
// 用户名            login_name          varchar(20)
// 创建时间          creater_date       varchar(20)
// 日志类型          log_type           int 0登录 1页面访问
// 数量              count              int 次数
// 是否上传          is_upload          int 0未上传 1已上传
export const INIT_LOG_TABLE_DB = (DB): Promise<any> => {
    return new Promise((resolve, reject) => {
        if (!DB) {
            reject(' DB null');
        } else {
            if (!DB.objectStoreNames.contains('salog')) {
                const objectStore = DB.createObjectStore('salog', {
                    keyPath: "id"
                });
                resolve(true)
            } else {
                resolve(true)
            }
        }
    });
}

export const ADD_ITEM_DB = (DB, item): Promise<any> => {
    return new Promise((resolve, reject) => {
        if (!DB) {
            reject(' DB null');
        } else {
            const request = DB.transaction([storeName], 'readwrite')
                .objectStore(storeName)
                .add(item);

            request.onsuccess = (event) => {
                resolve(event);
            };

            request.onerror = (err) => {
                reject(err)
            }
        }
    });
}
export const GET_ALL_ITEM = (db): Promise<any> => {
    return new Promise((resolve, reject) => {
        if (!db) {
            reject(' DB null');
        } else {
            let result = []
            let objectStore = db.transaction(storeName).objectStore(storeName);
            objectStore.openCursor().onsuccess = event => {
                let data = event.target.result;
                if (data) {
                    result.push(data.value);
                    data.continue();
                } else {
                    resolve(result.filter(v => v.is_upload === 0));
                }
            }
            objectStore.openCursor().onerror = event => {
                reject(event)
            }
        }
    });
}
export const DEL_ITEM_BYID = (db, id): Promise<any> => {
    return new Promise((resolve, reject) => {
        if (!db) {
            reject(' DB null');
        } else {
            let objectStore = db.transaction(storeName, 'readwrite').objectStore(storeName);
            let req = objectStore.delete(id)
            req.onsuccess = function () {
                resolve('删除成功');
            };

            req.onerror = function () {
                reject('删除失败');
            };
        }
    });
}
